﻿--[[
   @brief    gift_op_icon
   @author   harleyxwang
   @version  1.0
   @date     2016-12-22
--]]

GiftOpIcon = {
    icon_state_no_active = 0;
    icon_state_active_animation = 1;
    icon_state_active_no_animation = 2;
};

GiftOpIconEventListener = {
};


function GiftOpIcon:new(percent, wrapper_id, game_id, index)
    base_utility.log(string.format(
        "GiftOpIcon:new %d %d %d", percent, wrapper_id, game_id));
    local o = { percent_ = percent, wrapper_id_ = wrapper_id,
        game_id_ = game_id, index_ = index }
    setmetatable(o, self)
    self.__index = self
    o:init();
    return o;
end

function GiftOpIcon:init()
     self.icon_disable_url_ = "";
     self.icon_url = "";
     -- self.behavior_glint_ = ui_utility.get_ctrl_id_by_path(self.wrapper_id_, "BehaviorGlint");
end

function GiftOpIcon:init_icon_url(icon_diable_url, icon_url)
    if icon_diable_url ~= nil then
        self.icon_disable_url_ = icon_diable_url;
    end
    if icon_url ~= nil then
        self.icon_url = icon_url;
    end
end

function GiftOpIcon:log(msg, core_msg)
    base_utility.log(string.format("[GiftOpIcon][%d] %s", self.percent_, msg), core_msg);
end

-- state: 0: 未激活领取，1：激活领取，动画，2：激活，停止动画, 
function GiftOpIcon:set_icon_state(state)
    if self.state_ == state then
        return
    end

    self.state_ = state;
    if state == self.icon_state_no_active then
        local pro_value = "";
        if self.icon_disable_url_ ~= nil and self.icon_disable_url_ ~= "" then
            pro_value = string.format(
                "ONEIMAGE:main_bottom_button\\gift_icon_disable.png|%s;Center;null;", self.icon_disable_url_);
        else
            pro_value = "ONEIMAGE:main_bottom_button\\gift_icon_disable.png;Center;null;"
        end
        ui_utility.setproperty_by_id(self.wrapper_id_, "Background", pro_value);
        
        local behavior_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorStart");
        if behavior_ptr ~= nil then
            behavior_ptr:Stop();
        end
        
        local behavior_reverse_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorReverse");
        if behavior_reverse_ptr ~= nil then
            behavior_reverse_ptr:Stop();
        end    
    elseif state == self.icon_state_active_animation then
        local pro_value = "";
        if self.icon_url ~= nil and self.icon_url ~= "" then
            pro_value = string.format(
                "ONEIMAGE:main_bottom_button\\gift_icon.png|%s;Center;null;", self.icon_url);
        else
            pro_value = "ONEIMAGE:main_bottom_button\\gift_icon.png;Center;null;"
        end
        ui_utility.setproperty_by_id(self.wrapper_id_, "Background", pro_value);  

        local behavior_reverse_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorReverse");
        if behavior_reverse_ptr ~= nil then
            behavior_reverse_ptr:Stop();
        end   
       
        local behavior_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorStart");
        if behavior_ptr ~= nil then
            behavior_ptr:Stop();
            behavior_ptr:Start();
        end
    elseif state == self.icon_state_active_no_animation then
        local pro_value = "";
        if self.icon_url ~= nil and self.icon_url ~= "" then
            pro_value = string.format(
                "ONEIMAGE:main_bottom_button\\gift_icon.png|%s;Center;null;", self.icon_url);
        else
            pro_value = "ONEIMAGE:main_bottom_button\\gift_icon.png;Center;null;"
        end
        ui_utility.setproperty_by_id(self.wrapper_id_, "Background", pro_value); 
        
        local behavior_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorStart");
        if behavior_ptr ~= nil then
            behavior_ptr:Stop();
        end
        
        local behavior_reverse_ptr = ui_utility.get_ctrl_ptr_by_path(self.wrapper_id_, "BehaviorReverse");
        if behavior_reverse_ptr ~= nil then
            behavior_reverse_ptr:Stop();
        end   
    end
end

function GiftOpIcon:get_wrapper_id()
    return self.wrapper_id_;
end


------------------------------GiftOpIconEventListener------------------------------------------------

function GiftOpIconEventListener.on_mouse_hover(uilib, control_id, x, y)
    -- control_id is UIMessageSpy control id
    local item_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local item = gift_op_panel.get_gift_icon_item(item_ctrl_id);
    if nil == item then
        base_utility.log(string.format("[GiftOpIconEventListener][on_mouse_hover]item is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end
    -- 获取icon距离web左边的xOffset偏移：
    local icon_offset = 0;
    local icon_ptr = ui_utility.get_ctrl_ptr_by_id(item_ctrl_id);
    if icon_ptr ~= nil then
        local icon_rct = icon_ptr:GetRectEx(FALSE,TRUE);
        icon_offset = icon_rct.left;
    end
    local bottom_content_ptr = ui_utility.get_ctrl_ptr_by_path(frame_bottom_view.bottom_dlg_id_, "Bottom_Content_Pnl");
    if bottom_content_ptr ~= nil then
        local content_rect = bottom_content_ptr:GetRectEx(FALSE,TRUE);
        icon_offset = icon_offset + content_rect.left;
    end
   
    gift_op_panel.on_gift_icon_hover(item.game_id_, item.percent_, icon_offset);
end

function GiftOpIconEventListener.on_mouse_leave(uilib, control_id, x, y)
    local item_ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local item = gift_op_panel.get_gift_icon_item(item_ctrl_id);
    if nil == item then
        base_utility.log(string.format("[GiftOpIconEventListener][on_mouse_leave]item is NIL," ..
        " ctrl_id:%d", control_id), true);
        return;
    end
end

function GiftOpIconEventListener.on_behavior_end(uilib, control_id)
    local parent_id = ui_utility.get_ctrl_parent_id(control_id);
    local behavior_ptr = ui_utility.get_ctrl_ptr_by_path(parent_id, "BehaviorReverse");
    if behavior_ptr ~= nil then
        behavior_ptr:Stop();
        behavior_ptr:Start();
    end
end

function GiftOpIconEventListener.on_behavior_reverse_end(uilib, control_id)
    local parent_id = ui_utility.get_ctrl_parent_id(control_id);
    local behavior_ptr = ui_utility.get_ctrl_ptr_by_path(parent_id, "BehaviorStart");
    if behavior_ptr ~= nil then
        behavior_ptr:Stop();
        behavior_ptr:Start();
    end
end